<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>非工单领料申请编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script type="text/javascript">
	var basic_Url = "${ctx}/yd/logic/progetapply";
	$(function() {
		$("#inputForm").validate({
			onsubmit : true,
			/*执行ajaxsubmit  */
			submitHandler : function(editform) {
				try {
					url = basic_Url + "/save.do";
					var api = frameElement.api;
					var W = api.opener;

					getReturnMessage(url, "inputForm", W.searchAP);
				} catch (e) {
					alert(e);
				}
			}
		});
		
		if ('${viewflag}' == "add"){
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#tr1").hide();
			$("#wanjie").hide();  //禁用完结按钮
			$("#applyExportExcel").hide();
		}else if ('${viewflag}' == "tijiao"){
			$("#save").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#wanjie").hide();  //禁用完结按钮
            if($("#dcruser").val()!="${user.name}"){
    			$("#save1").hide();
    			$("input[type!='hidden']").attr("disabled","disabled");
    			$("select").attr("disabled","disabled");//禁用SELECT标签
    			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
               } 
		}else if ('${viewflag}' == "shenpi"){
			$("#save").hide();
			$("#save1").hide();
			$("#wanjie").hide();  //禁用完结按钮
			$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}else if ('${viewflag}' == "tongguo"){
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save").hide();
			if('${user.name}'!='admin'){
				if($("#dcruser").val()!="${user.name}"){ 
					$("#wanjie").hide();  //禁用完结按钮
				}else{
					if("${oi.isnotck}"=="2"){
						$("#wanjie").hide();  //禁用完结按钮
					}
				}
			}else{
				if("${oi.isnotck}"=="2"){
					$("#wanjie").hide();  //禁用完结按钮
				}
			}
			$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}
	})
	function submitForm(param) {
	 	$("#billStatus").val(param);
	 	$("#addressInfos").val(JSON.stringify($("#dataGrid1").ligerGetGridManager().getData()));
		var data = $("#dataGrid1").ligerGetGridManager().getData();
		if(data.length==0){
			$.dialog.alert("请增加物料信息");
	   	 	return false;
		}
		var ba = true;
		if(data !=null){
			for (var i = 0;i < data.length;i++){
				var row = data[i];
				if (undefined == row.unit|| null == row.unit||row.unit=="") {
					$.dialog.alert("第" + (i+1) + "行的单位没有选择");
					ba = false;
					return false;
				}
				if(undefined == row.quantuty
						|| isNaN(row.quantuty)
						|| row.quantuty<= 0){
	           	 $.dialog.alert("您第"+(i+1)+"行的出库数量不合法");
	           	 ba = false;
	           	 return false;
	            }
			}
		}
		
		
		//设置ajax全局同步
		jQuery.ajaxSettings.async = false;
		if(ba){
			$("#submitFlag").val(param);
			$("#inputForm").submit();
		}
	}

	function tableSubmit(msg,param){
		$.ligerDialog.confirm(msg, 
				function (flag) { 
				if(flag){
					submitForm(param);
					}
			}); 
	}

	function submitpass(param){
		tableSubmit('是否确认审核通过？',param);
	}
	function submitend(param){
		tableSubmit('是否确认完结此申请单？',param);
	}
	function getA(){
		$("#whName").val($("#whCode").find("option:selected").text());
	}
	
	function check(){
		$.ajax({
		      url: "${ctx}/yd/logic/progetapply/updateState.do",
		      global: false,
		      type: "POST",
		      dataType: "json",
		      data: ({
		    	   rows:$("#id").val()
		      }),		     
		      success: function(msg){
		    	  if(msg.msg=="SUCCESS"){
		    		  $.dialog.alert("驳回成功",function(){close();});
		    	  }
		    	  if(msg.msg=="ERROR"){
		    		  $.dialog.alert("操作失败",function(){close();});
		    	  }
		   }
		});
	}
	
	function close(){
		var api = frameElement.api;
		var W = api.opener;
		CloseDialog();
		W.searchAP();
	}
	
	/**
	 * 导出
	 */
	function exportExcel() {
		var path = basic_Url + "/exportExcel.do?id=${oi.id}";
		window.open(path, "_blank");
	}
</script>

</head>

<body>

	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${oi.id}" />
		<input type="hidden" id="addressInfos" name="addressInfos"/>
		<input type="hidden" id="dataItem" name="dataItem"/>
		<input type="hidden" id="batchItem" name="batchItem"/>
		<input type="hidden" id="varsion" name="varsion" value="${oi.varsion}"/>
		<input type="hidden" id="billStatus" name="billStatus" value="${oi.billStatus}"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock" align="center">
					<tr id="tr1">
					 	<td nowrap="nowrap" width="20%">单号:</td>
						<td width="30%"><input id="getProCode" name="getProCode" class="inp_width_98p" readonly="readonly" value="${oi.getProCode}" /></td>
						<td nowrap="nowrap" width="20%">建单时间:</td>
						<td width="30%"><input class="inp_width_98p" type="text" id="dctime" name="dctime"
							value="<fmt:formatDate value="${empty oi.dctime?nowDate:oi.dctime}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/></td>
					</tr>
					<tr >
					  <td nowrap="nowrap" width="20%">领用人:</td>
						<td width="30%">
						    <input type="text" id="aegnt" name="aegnt" class="inp_width_98p required" value="${oi.aegnt}" />
							<c:if test="${oi.aegnt != null}">
								<script>
									$("#aegnt").val("${oi.aegnt}");
								</script>
							</c:if>
					  </td>
					  <td nowrap="nowrap" width="20%">制单人:</td>
						<td width="30%">
						    <input type="text" id="dcruser" name="dcruser" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
							<c:if test="${oi.dcruser != null}">
								<script>
									$("#dcruser").val("${oi.dcruser}");
								</script>
							</c:if>
					  </td>
					</tr>
					<tr>
						<td >领用原因:</td>
						<td colspan="3"><input id="remark" name="remark" class="inp_width_98p" value="${oi.remark}" /></td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid1"></div></td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
		        	 <li><div class="button"><div class="buttonContent"><button type="button" id="applyExportExcel" class="export" onclick="exportExcel()">导出</button></div></div> </li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm(0)">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm(1)">提交</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="wanjie" onclick="submitend(3)">完结订单</button></div></div></li>
		        <webres:displaycontroller elementId="LOGIC_APPLY_GET_SP" description="领料申请审核">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitpass(2)">审核通过</button></div></div></li>
			    </webres:displaycontroller>
			    <webres:displaycontroller elementId="LOGIC_APPLY_GET_PH" description="领料申请驳回">   
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="check()">驳回</button></div></div></li>
		       	</webres:displaycontroller>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
		<script type="text/javascript">
		var ItemStateList = ItemStateData.Rows;
		var addressGrid;
    var column1 = [
    { display: 'id', name: 'id',type: 'string',hide:1},
    { display: 'materialsid', name: 'materialsid',hide:1,width:1},
    { display: '<font color="gray">编码</font>', name: 'code',width: 120},
    { display: '<font color="gray">名称</font>', name: 'name',width: 120},
    { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
    { display: '<font color="gray">物料是否可用批次</font>', name: 'openBatch' ,hide:true,width: 150, render: function (item, rowindex, value, column)
	    {
	   	if(item.openBatch=="Y"){
	   		return "可用";
	   	}else{
	   		return "不可用";
	   	}
   		
}},
    { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
    { display: '<font color="gray">基本单位出库数量</font>', name: 'jquantuty',width: 150},
    { display: '单位<font color="red">*</font>', name: 'unit', width: 100,editor : {
	 	   type:'selecturl',dataItem:'dataItem',path:'${ctx}/yd/basic/measurementUnit/unitlist.do',valueColumnName: 'unitname', displayColumnName: 'unitname'}, render: function (item)
        {
	 		  var dataitem = $.parseJSON($("#dataItem").val());
	 		  if(dataitem!=null){
	 			  for (var i = 0; i < dataitem.length; i++)
	 			  {
	                  if (dataitem[i]['unitname'] == item.unit&&dataitem[i]['materialscode']==item.code){
	                 	 addressGrid.updateCell('unitconvert', dataitem[i]['unitconvert'], item);
	                      return dataitem[i]['unitname'];
	                  }
	              }
	 		  }
            	return item.unit;
        }},
    { display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
	{ display: '出库数量<font color="red">*</font>', name: 'quantuty',width: 150,editor : {type :'float'}, render: function (item)
    {
	   if(item.quantuty!=''&&item.quantuty!=null&&item.unitconvert!=null&&item.unitconvert!=''){
	   		addressGrid.updateCell('jquantuty', item.quantuty*item.unitconvert, item);
	   }
       return item.quantuty;
    }}];
    
    var column2 = [{ display: 'id', name: 'id',type: 'string',hide:1},
                   { display: 'materialsid', name: 'materialsid',hide:1,width:1},
                   { display: '<font color="gray">编码</font>', name: 'code',width: 120},
                   { display: '<font color="gray">名称</font>', name: 'name',width: 120},
                   { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
                   { display: '<font color="gray">物料是否可用批次</font>', name: 'openBatch' ,hide:true,width: 150, render: function (item, rowindex, value, column)
					    {
	            	   	if(item.openBatch=="Y"){
	            	   		return "可用";
	            	   	}else{
	            	   		return "不可用";
	            	   	}
				   		
			    }},
                   { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
                   { display: '<font color="gray">基本单位出库数量</font>', name: 'jquantuty',width: 150},
                   { display: '单位<font color="red">*</font>', name: 'unit' ,width: 150},
                   { display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
                   { display: '出库数量<font color="red">*</font>', name: 'quantuty',width: 150}];
                
    
    var column3 = null;
    if ('${xx}' == 1){
    	column3 = column1;
    }else 
    	column3 = column2;
    
    addressGrid = $("#dataGrid1").ligerGrid({
		 columns: column3,
	                    enabledEdit : true,
	                    usePager: true,
	      				width : '99%',
	      				pageSize:1000,
	      				pageSizeOptions:[1000],
	      				parms:[{ name : 'id' , value : '${oi.id}'}],
	      		      	url : basic_Url+"/griditems.do",
	      				rownumbers : false,
	      				checkbox : true,
	                    height : '90%',
		toolbar : {
			items : [ {
				text : '新增',
				disable : true,
				click : function() {
					if ('${xx}' == 1){
							getDG().dialog({
					    		title:'选择材料',
					    		width : 800,
								height : 400,
								content: 'url:' + sys_context+'/public/windowalert/itemmanager.do',//
					    		lock : true,
					    		max : true,
					    		min : true,
					    		ok: function(){
					    			var rows = this.content.getSelected();
									if (rows != null) {
										for (var i = 0; i < rows.length;i++){
											var row = rows[i];
											addressGrid.addRow2({
												code : row.code,
	   											name : row.name,
	   											spec : row.spec,
	   											materialsid:row.id,
	   											junit : row.unit,
	   											openBatch : row.remark2
											});
										}
									}
					    			this.content.Close();
					        		return false;
					    	    },
					    	    cancelVal: '关闭',
					    	    cancel: true 
					    	}).zindex();
						}
				},
				icon: 'add'
			}, {
				line : true
			}, {
				text : '删除',
				click : function() {
					if ('${xx}' == 1){
					addressGrid.deleteSelectedRow();
					}
				},
				img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
			}]
		}});
	
	
	/* <c:forEach items="${axxJson}" var="row">
	addressGrid.addRow2({
			id : "${row.id}",
	   		materialsid:"${row.materialsid}",
	   		code : "${row.code}",
	   		name : "${row.name}",
	   		spec : "${row.spec}",
	   		junit : "${row.junit}",
	   		unit : "${row.unit}",
	   		unitconvert : "${row.unitconvert}",
	   		quantuty : "${row.quantuty}",
	   		jquantuty : "${row.jquantuty}",
	   		openBatch : "${row.openBatch}"
	});
    </c:forEach> */
    
    </script>
	</form>
</body>
</html>
